package com.epsoft.odin7cj.flow.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 用户任务
 * @author Administrator
 *
 */
public class T01UserTask {
	
	public static void main1(String[] args) {
		
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		
		RuntimeService runs = engine.getRuntimeService();
		
		TaskService ts = engine.getTaskService();
		
		RepositoryService rs = engine.getRepositoryService();
		
		
		IdentityService ids = engine.getIdentityService();
		
		
		/**		
		Group bossGroup = ids.newGroup("boss");
		bossGroup.setName("boss_yang");
		
		Group managementGroup = ids.newGroup("management");
		managementGroup.setName("management_xia");
		
		User huUser = ids.newUser("bruceHu");
		
		huUser.setFirstName("测试");
		huUser.setLastName("胡");
		
		ids.saveGroup(bossGroup);
		ids.saveGroup(managementGroup);
		ids.saveUser(huUser);
		*/
		
		Deployment deploy = rs.createDeployment().addClasspathResource("bpmn/t01-user-task.bpmn").deploy();
		
		ProcessDefinition pd = rs.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
		
		// 设置启动参数，在 bpmn 配置文件中通过 JUEL 表达式获取
		Map<String,Object> varMap = new HashMap<String,Object>();
		List<String> userIdList = new ArrayList<String>();
		userIdList.add("bruceHu");
		varMap.put("users", userIdList);
		
		ProcessInstance ps = runs.startProcessInstanceById(pd.getId(),varMap);
		
		
		Task task = ts.createTaskQuery().processInstanceId(ps.getId()).singleResult();
		
		System.out.println("\n 根据实例ID（"+ps.getId()+"）查询任务："+task.getName());
		
		List<Task> taskList = ts.createTaskQuery().taskCandidateUser("bruceHu").list();
		
		StringBuffer sbTask = new StringBuffer();
		for(Task tmpTask : taskList) {
			sbTask.append(tmpTask.getName()+",");
		}
		System.out.println("\n 根据用户(bruceHu) 查询任务："+sbTask);
		
		taskList = ts.createTaskQuery().taskCandidateGroup("management").list();
		sbTask = new StringBuffer();
		for(Task tmpTask : taskList) {
			sbTask.append(tmpTask.getName());
		}
		System.out.println("\n 根据用户组(management)查询任务："+sbTask);
		
		taskList = ts.createTaskQuery().taskCandidateGroup("boss").list();
		sbTask = new StringBuffer();
		for(Task tmpTask : taskList) {
			sbTask.append(tmpTask.getName());
		}
		System.out.println("\n 根据用户组(boss)查询任务："+sbTask);
	}

}
